package pl.alef.mindplay.dao.impl.jpa;

import java.util.List;

import javax.persistence.NamedQuery;
import javax.persistence.criteria.CriteriaQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import pl.alef.mindplay.dao.VisitDAO;
import pl.alef.mindplay.model.Visit;


@Repository("visitDAO")
public class VisitDAOImpl extends BaseDAO implements VisitDAO {
	
	final static Logger logger = LoggerFactory.getLogger(VisitDAOImpl.class);
	
	
	public Visit save(Visit visit) {
		if(visit.getId() != null) {
			getEntityManager().merge(visit);			
		} else {
			getEntityManager().persist(visit);
		}
		return visit;
	}
	
	public List<Visit> queryAll() {		
		//CriteriaQuery<Visit> query = getEntityManager().getCriteriaBuilder().createQuery(Visit.class);
		//List<Visit> list = getEntityManager().createQuery(query).getResultList();
		List<Visit> list = getEntityManager().createNamedQuery("Visit.all.with.addr", Visit.class).getResultList();
		logger.debug(" queryAll - found {} records", list.size());
		return list;
	}
}
